const MD5 = require('js-md5');
const { RetCodeMsg } = require('./util.js'); //  计数器
let counter = 1;
function getCounter() {
    ++counter;
    if (counter < 10) return '00000' + counter;
    else if (counter < 100) return '0000' + counter;
    else if (counter < 1000) return '000' + counter;
    else if (counter < 10000) return '00' + counter;
    else if (counter < 100000) return '0' + counter;
    else if (counter < 1000000) return counter;
    else {
        counter = 1;
        return '00000' + counter;
    }
}
/**
 * 字符串转成字节
 * @param str
 * @returns {[]}
 */
function str2Byte(str) {
    let bytes = [];
    let length = str.length;
    let char;
    for (let i = 0; i < length; i++) {
        char = str.charCodeAt(i);
        if (char >= 0x010000 && char <= 0x10ffff) {
            bytes.push(((char >> 18) & 0x07) | 0xf0);
            bytes.push(((char >> 12) & 0x3f) | 0x80);
            bytes.push(((char >> 6) & 0x3f) | 0x80);
            bytes.push((char & 0x3f) | 0x80);
        } else if (char >= 0x000800 && char <= 0x00ffff) {
            bytes.push(((char >> 12) & 0x0f) | 0xe0);
            bytes.push(((char >> 6) & 0x3f) | 0x80);
            bytes.push((char & 0x3f) | 0x80);
        } else if (char >= 0x000080 && char <= 0x0007ff) {
            bytes.push(((char >> 6) & 0x1f) | 0xc0);
            bytes.push((char & 0x3f) | 0x80);
        } else {
            bytes.push(char & 0xff);
        }
    }
    return bytes;
}
function changeByte(encryptStr, movedCard) {
    let encryptByte = str2Byte(encryptStr);
    let length = encryptByte.length;
    let temp;
    for (let i = 0; i < length; i++) {
        temp = i % movedCard > (length - i) % movedCard ? encryptByte[i] : encryptByte[length - (i + 1)];
        encryptByte[i] = encryptByte[length - (i + 1)];
        encryptByte[length - (i + 1)] = temp;
    }
    return encryptByte;
}
function mergeByte(encryptByte, changeByte) {
    let length = encryptByte.length;
    let doubleLength = length * 2;
    let temp = new Array(doubleLength);
    for (let i = 0; i < length; i++) {
        temp[i] = encryptByte[i];
        temp[doubleLength - 1 - i] = changeByte[i];
    }
    return temp;
}
export class Signature {
    static TMS = getCounter() + new Date().getTime().toString();
    constructor(tms) {
        this.tms = tms || Signature.TMS;
    }
    get(uuid, appKey, appSecret, movedCard) {
        let encryptStr = uuid + appKey + appSecret + this.tms;
        let encryptByte = str2Byte(encryptStr);
        let _changeByte = changeByte(encryptStr, movedCard);
        let _mergeByte = mergeByte(encryptByte, _changeByte);
        return MD5(_mergeByte);
    }
}
export function rdsFilter(request) {
    return request.then((res) => {
        let ret;
        const { code } = res;
        switch (code) {
            case 2000:
                ret = res.data.Ret;
                if ((ret && ret === 100) || ret === 104) return Promise.resolve(res.data);
                else {
                    uni.$fd.message.error(RetCodeMsg[ret]);
                    return Promise.reject();
                }
            case 5000:
                ret = res.data.result.errno;
                uni.$fd.message.error(RetCodeMsg[ret]);
                return Promise.reject();
            default:
                ret = res.data.result.errno;
                uni.$fd.message.error(RetCodeMsg[ret]);
                return Promise.reject();
        }
    });
}
